import os
import shutil
from Common.excel import excel


# 判断文件夹是否存在，不存在创建
def create_folder(file, path):
    folder_path = os.path.join(path,file)
    if os.path.exists(folder_path):
        pass
    else:
        os.mkdir(folder_path)
    return folder_path

# 复制文件到新目录下
def copy_file(file1,file2):
    src_file = file1
    dst_file = file2
    shutil.copy2(src_file, dst_file)


def has_multiple_id_front(lst,str):
    # 筛选出以“身份证正面”开头的元素
    id_front_items = [item for item in lst if item.startswith(str)]
    # 统计数量并判断是否 ≥ 2
    return len(id_front_items)



# 文件夹路径
desktopPath = input('待分类文件路径：')
excelpath = input('获取手机号excel地址:')
oo = excel(excelpath)
#desktopPath = '/Users/chenliang/Desktop/测试文件'
#文件存入地址
#folderpath = input('文件存放地址：')
#folderpath = '/Users/chenliang/Desktop/ABC'

#取出文件名，放入列表
allTtems = os.listdir(desktopPath)
desktopPathNew = desktopPath + '_' + 'New'

# 创建目录
os.makedirs(desktopPathNew, exist_ok=True)

#print(allTtems)
# /Users/chenliang/Documents/Python_workhome/Ansible/tool/司机材料_New
# /Users/chenliang/Desktop/司机车辆通用导入模板 (普通批量).xlsx
for item in allTtems:
    try:
        if item !=".DS_Store":
            # 文件名，文件路径
            filename = os.path.basename(item)
            filepath = os.path.join(desktopPath,filename)
            filenameList = list(filename.split("_",2))
            #print(filenameList)
            driver_list,sheet_num=oo.findData(0,filenameList[0])
            #print(driver_list)
            fileTiems = os.listdir(desktopPath+'/'+item)
            #print(fileTiems)
            ee = ''
            for file in fileTiems:
                ee = ''
                try:
                    if "身份证正面" in file and has_multiple_id_front(fileTiems,'身份证正面')==1:
                        old_name = os.path.join(desktopPath+'/'+item, file)
                        #new_name = os.path.join(desktopPath+'/'+item, filenameList[2]+'-1.jpg')
                        new_name = os.path.join(desktopPathNew, driver_list[1]+'-1.jpg')
                        os.rename(old_name, new_name)
                        #print(f"重命名：{old_name} → {new_name}")
                    if "身份证反面" in file and has_multiple_id_front(fileTiems,'身份证反面')==1:
                        old_name = os.path.join(desktopPath+'/'+item, file)
                        #new_name = os.path.join(desktopPath+'/'+item, filenameList[2]+'-2.jpg')
                        new_name = os.path.join(desktopPathNew, driver_list[1]+'-2.jpg')
                        os.rename(old_name, new_name)
                        #print(f"重命名：{old_name} → {new_name}")
                    if "驾驶证正面" in file and has_multiple_id_front(fileTiems,'驾驶证正面')==1:
                        old_name = os.path.join(desktopPath + '/' + item, file)
                        #new_name = os.path.join(desktopPath + '/' + item, filenameList[2] + '-3.jpg')
                        new_name = os.path.join(desktopPathNew, driver_list[1]+'-3.jpg')
                        os.rename(old_name, new_name)
                        #print(f"重命名：{old_name} → {new_name}")
                    if "驾驶证副面" in file and has_multiple_id_front(fileTiems,'驾驶证副面')==1:
                        old_name = os.path.join(desktopPath + '/' + item, file)
                        #new_name = os.path.join(desktopPath + '/' + item, filenameList[2] + '-4.jpg')
                        new_name = os.path.join(desktopPathNew, driver_list[1] + '-4.jpg')
                        os.rename(old_name, new_name)
                        #print(f"重命名：{old_name} → {new_name}")
                    if "行驶证正面" in file and has_multiple_id_front(fileTiems,'行驶证正面')==1:
                        old_name = os.path.join(desktopPath + '/' + item, file)
                        #new_name = os.path.join(desktopPath + '/' + item, filenameList[1] + '-1.jpg')
                        new_name = os.path.join(desktopPathNew, filenameList[1] + '-1.jpg')
                        os.rename(old_name, new_name)
                        #print(f"重命名：{old_name} → {new_name}")
                    if "行驶证副面" in file and has_multiple_id_front(fileTiems,'行驶证副面')==1:
                        old_name = os.path.join(desktopPath + '/' + item, file)
                        #new_name = os.path.join(desktopPath + '/' + item, filenameList[1] + '-2.jpg')
                        new_name = os.path.join(desktopPathNew, filenameList[1] + '-2.jpg')
                        os.rename(old_name, new_name)
                        #print(f"重命名：{old_name} → {new_name}")
                    if "道路运输证正面" in file and has_multiple_id_front(fileTiems,'道路运输证正面')==1:
                        old_name = os.path.join(desktopPath + '/' + item, file)
                        #new_name = os.path.join(desktopPath + '/' + item, filenameList[1] + '-4.jpg')
                        new_name = os.path.join(desktopPathNew, filenameList[1] + '-4.jpg')
                        os.rename(old_name, new_name)
                        #print(f"重命名：{old_name} → {new_name}")
                    if "从业资格证正面" in file and has_multiple_id_front(fileTiems,'从业资格证正面')==1:
                        old_name = os.path.join(desktopPath + '/' + item, file)
                        #new_name = os.path.join(desktopPath + '/' + item, filenameList[2] + '-5.jpg')
                        new_name = os.path.join(desktopPathNew, driver_list[1] + '-5.jpg')
                        os.rename(old_name, new_name)
                        #print(f"重命名：{old_name} → {new_name}")
                    if has_multiple_id_front(fileTiems,'行驶证正面')>=2:
                        ee = os.path.join(desktopPath+'/'+item, file) +'==='+'存在多个需手动处理'
                    else:
                        pass
                except Exception as e:
                    print(f"处理失败：{old_name} → 原因：{str(e)}")
            if ee!='':
                print(ee)

        else:
            continue
    except Exception as e:
        print(f"处理失败：{item} → 原因：{str(e)}")
        continue